<template>
    <el-dialog :title="action == 'add' ? '新增' : '编辑'" v-model="showModal">
        <el-form ref="FormRef" :model="FormData" label-width="100px" :rules="rules">
            <el-form-item label="用户名" prop="userName">
                <el-input v-model="FormData.userName" placeholder="请输入用户名称" :disabled="action == 'edit'" />
            </el-form-item>
            <el-form-item label="用户昵称" prop="nickName">
                <el-input v-model="FormData.nickName" placeholder="请输入用户昵称" />
            </el-form-item>

            <el-form-item label="邮箱" prop="userEmail">
                <el-input v-model="FormData.userEmail" :disabled="action == 'edit'" placeholder="请输入用户邮箱">
                    <template #append>@imooc.com</template>
                </el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
                <el-input v-model="FormData.mobile" placeholder="请输入手机号" />
            </el-form-item>
            <el-form-item label="岗位" prop="job">
                <el-input v-model="FormData.job" placeholder="请输入岗位" />
            </el-form-item>
            <el-form-item label="状态" prop="state">
                <el-select v-model="FormData.state">
                    <el-option :value="1" label="在职"></el-option>
                    <el-option :value="2" label="离职"></el-option>
                    <el-option :value="3" label="试用期"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="系统角色" prop="roleList">
                <el-select v-model="FormData.roleList" placeholder="请选择用户系统角色" multiple style="width: 100%">
                    <el-option v-for="role in roleList" :key="role._id" :label="role.roleName"
                        :value="role._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="部门" prop="deptId">
                <el-cascader v-model="FormData.deptId" placeholder="请选择所属部门" :options="deptList"
                    :props="{ checkStrictly: true, value: '_id', label: 'deptName' }" clearable
                    style="width: 100%"></el-cascader>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, defineProps, getCurrentInstance, onMounted } from 'vue'
const { proxy } = getCurrentInstance();
const $Tajax = proxy.$Tajax;
const showModal = ref(true)
const props = defineProps({
    Options: {
        type: Array,
        default: () => []
    },
    action: {
        type: String,
        default: 'add'
    },
    Item: {
        type: Object,
        default: ''
    },

})
const FormRef = ref(null)
const FormData = ref({
    parentId: [null],
    userId: null,
    deptName: null,
    user: null,
    userEmail: null,
})

onMounted(() => {
    if (props.action == 'edit') {
        FormData.value = props.Item
    } else {
        FormData.value.parentId = props.Item._id
        console.log(FormData.value.parentId)
    }
    getDeptList()
    getRoleAllList()
})
const deptList = ref([]) // 部门列表
const getDeptList = async () => {
    let res = await $Tajax.getDeptList();
    deptList.value = res
}
const roleList = ref([]); // 角色列表
const getRoleAllList = async () => {
    let list = await proxy.$Tajax.getRoleAllList();
    roleList.value = list;
};
const emit = defineEmits(['close'])

const rules = ref({
    userName: [
        {
            required: true,
            message: "请输入登陆账号",
            trigger: "blur",
        },
    ],
    nickName: [
        {
            required: true,
            message: "请输入用户姓名",
            trigger: "blur",
        },
    ],
    userEmail: [
        { required: true, message: "请输入用户邮箱", trigger: "blur" },
    ],
    mobile: [
        {
            pattern: /1[3-9]\d{9}/,
            message: "请输入正确的手机号格式",
            trigger: "blur",
        },
    ],
    deptId: [
        {
            required: false,
            message: "请选择部门",
            trigger: "blur",
        },
    ],
})

const submitForm = () => {
    FormRef.value.validate(async (valid) => {
        if (valid) {
            console.log(FormData.value)
            let params = { ...FormData.value, action: props.action };
            let res = await $Tajax.userSubmit(params);
            proxy.$message.success("用户创建成功");
            emit('close', true)
            FormRef.value.resetFields()
        }
    })
}

const cancel = () => {
    emit('close', false)
}

</script>

<style></style>